package com.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import model.CompClaimDTO;
import model.CompensationLeaveDTO;
import model.UserDTO;
import bizlogic.CompensationLeaveManager;
import bizlogic.UserManager;
import exception.DAOSystemException;

import util.Validation;

/**
 * @pranjal
 * Servlet implementation class ProcessCompensationClaim
 */
@WebServlet("/ProcessCompensationClaim")
public class ProcessCompensationClaim extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private Logger logger = Logger.getLogger(this.getClass());
	private RequestDispatcher claimJsp;

	@Override
	public void init(ServletConfig config) throws ServletException {
		ServletContext context = config.getServletContext();
		claimJsp = context.getRequestDispatcher("/SubordinateCompClaim.jsp");
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			doProcess(request,response);
		} catch (DAOSystemException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			doProcess(request,response);
		} catch (DAOSystemException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void doProcess(HttpServletRequest req,
			HttpServletResponse resp) throws ServletException, IOException, DAOSystemException {
		String action = null;
		String validation= Validation.validate(req,resp);
		
			if(req.getParameter("action") != null)
			{
				action = (String) req.getParameter("action").trim();
			
				if (action.equals("reject")) {
					doReject(req);
					req.setAttribute("state", "delete");	
					doRefresh(req, resp);
				}
				else if (action.equals("approve")) {
					doApprove(req);
					req.setAttribute("state", "add");	
					doRefresh(req, resp);
				}
			} else {
					req.setAttribute("state", "none");	
					doRefresh(req, resp);
				}
		
	}

	private void doRefresh(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, DAOSystemException {
		
		UserDTO us =(UserDTO)req.getSession().getAttribute("user");
		String id = us.getId().toString();
		CompensationLeaveManager compLeaveMan = new CompensationLeaveManager();
		ArrayList<CompensationLeaveDTO> compList = new ArrayList<CompensationLeaveDTO>();
		compList = compLeaveMan.findAllCompensationLeave();
		ArrayList<CompClaimDTO> subList = new ArrayList<CompClaimDTO>();

		
		for(CompensationLeaveDTO comp : compList)
		{
			UserManager userman = new UserManager();
			UserDTO subordinate = userman.findUser(comp.getId());
			if(subordinate.getManager()!=null)
				if(subordinate.getManager().getId().equalsIgnoreCase(us.getId()))
				{
					CompClaimDTO claim = new CompClaimDTO();
					claim.setId(comp.getId());
					claim.setName(subordinate.getName());
					claim.setBalanceOT(comp.getBalanceOT());
					subList.add(claim);
				}
		}
		
		req.setAttribute("Datas", subList);
		claimJsp.forward(req, resp);
		
	}

	private void doApprove(HttpServletRequest req) {
		CompensationLeaveManager compLeaveMan = new CompensationLeaveManager();
		CompensationLeaveDTO upd = compLeaveMan.findCompensationLeave(req.getParameter("id"));
		double balance = upd.getBalanceOT();
		double initial = upd.getTotalOT();
		upd.setTotalOT(balance+initial);
		upd.setBalanceOT(0);
		compLeaveMan.updateCompensationLeave(upd);
		
	}

	private void doReject(HttpServletRequest req) {
		CompensationLeaveManager compLeaveMan = new CompensationLeaveManager();
		CompensationLeaveDTO upd = compLeaveMan.findCompensationLeave(req.getParameter("id"));
		upd.setBalanceOT(0);
		compLeaveMan.updateCompensationLeave(upd);
		
	}
}
